@include ('user.header', ['title' => '编辑域名'])

<div class="container">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="/user">用户中心</a></li>
            <li class="breadcrumb-item"><a href="/user/domain-list">短网址域名</a></li>
            <li class="breadcrumb-item active" aria-current="page">编辑域名</li>
        </ol>
    </nav>
</div>

<div class="container">
    <div class="row">
        <div class="col-md-8">
            @if (session('status'))
                @if (session('status') == '0' || session('status') == '2')
                    <div class="alert alert-warning alert-dismissible fade show" role="alert">
                        {{ session('msg') }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                @else
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                        {{ session('msg') }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                @endif
            @endif

            @if (isset($domain))
                <div class="card shadow-sm mb-3">
                    <div class="card-body p-3">
                        <form action="/api/domain/edit/{{$domain['id']}}" 
                              method="post" 
                              class="row g-3" 
                              role="form" 
                              id="editForm">
                            <input type="hidden" name="api_token" value="{{session('user')->api_token}}">
                            @csrf
                            <div class="col-md-12">
                                <label for="domain" class="form-label">域名</label>
                                <input type="text" id="domain" value="{{ $domain['domain'] }}" class="form-control" disabled="disabled"/>
                            </div>
                            <div class="col-md-12">
                                <label for="share" class="form-label">是否共享</label>
                                <select name="share" id="share" class="form-select">
                                    <option value="0" @if ($domain['share'] == '0') selected="selected" @endif>不共享</option>
                                    <option value="1" @if ($domain['share'] == '1') selected="selected" @endif>共享</option>
                                </select>
                            </div>
                            <div class="col-md-12">
                                <label for="link_length" class="form-label">默认后缀长度</label>
                                <input type="number" name="link_length" id="link_length" value="{{ $domain['link_length'] }}" class="form-control" required/>
                            </div>
                            <div class="col-md-12">
                                <button type="submit" class="btn btn-primary">更改</button>
                            </div>
                        </form>

                        <script>
                            $(document).ready(function() {
                                $('#editForm').on('submit', function(e) {
                                    e.preventDefault(); // 阻止表单默认提交行为

                                    $.ajax({
                                        url: $(this).attr('action'),
                                        type: 'POST',
                                        data: $(this).serialize(),
                                        success: function(response) {
                                            if (response.status == '1') {
                                                // 显示成功消息
                                                $('.alert').remove(); // 移除之前的消息
                                                $('#editForm').before('<div class="alert alert-success alert-dismissible fade show" role="alert"><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' + response.msg + '</div>');
                                            } else {
                                                // 显示错误消息
                                                $('.alert').remove(); // 移除之前的消息
                                                $('#editForm').before('<div class="alert alert-warning alert-dismissible fade show" role="alert"><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' + response.msg + '</div>');
                                            }
                                        },
                                        error: function(xhr) {
                                            // 处理请求失败的情况
                                            $('.alert').remove(); // 移除之前的消息
                                            $('#editForm').before('<div class="alert alert-danger alert-dismissible fade show" role="alert"><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>请求失败，请稍后重试</div>');
                                        }
                                    });
                                });
                            });
                        </script>
                    </div>
                </div>
            @endif
        </div>

        <div class="col-md-4">
            <div class="card shadow-sm">
                <div class="card-body">
                    <h5 class="card-title">共享说明</h5>
                    <p class="card-text"><b>共享</b><br/>其他用户也可以用此后缀进行生成短链接（不能自定义后缀），你对其后缀的短链接有管理权。</p>
                   @if ($domain['share'] == '1')
                   <p class="card-text"><b></p><b>共享链接</b><br/><a href="http://{{admin_setting('host')}}/dwz/{{ $domain['id'] }}">http://{{admin_setting('host')}}/dwz/{{ $domain['id'] }}</a></p>
                   @endif
                   
                    <hr/>
                    <p class="card-text"><b>不共享</b><br/>纯私人使用。</p>
                </div>
            </div>
        </div>
    </div>
</div>

@include('user.footer')